/*
编写 : Hexh
时间 : 2012-5-25 17:19:10
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsIntlVehicleAppl
	/// </summary>
	public class FsIntlVehicleApplController : BaseController
	{

		private readonly IFsIntlVehicleApplFacade FsIntlVehicleApplFacade;
		private readonly IHospUserFacade HospUserFacade;
		private readonly IInfraApproveFacade InfraApproveFacade;

		public FsIntlVehicleApplController(IFsIntlVehicleApplFacade FsIntlVehicleApplFacade, IHospUserFacade HospUserFacade, IInfraApproveFacade InfraApproveFacade)
		{
			this.FsIntlVehicleApplFacade = FsIntlVehicleApplFacade;
			this.HospUserFacade = HospUserFacade;
			this.InfraApproveFacade = InfraApproveFacade;
		}

		[Description("访问公务派车-管理页面")]
		[ViewPage]
		public ActionResult FsIntlVehicleAppl()
		{
			return View();
		}

		[Description("访问公务派车-查询页面")]
		[ViewPage]
		public ActionResult ViewIntlVcApplQuery()
		{
			return View();
		}

		[Description("访问公务派车-用车申请页面")]
		[ViewPage]
		public ActionResult ViewIntlVcApplAdd()
		{
			return View();
		}

		[Description("访问公务派车-用车审批页面")]
		[ViewPage]
		public ActionResult ViewIntlVcApplAprove()
		{
			return View();
		}

		[Description("访问公务派车-出发登记页面")]
		[ViewPage]
		public ActionResult ViewIntlVcApplSend()
		{
			return View();
		}

		[Description("访问公务派车-返回登记页面")]
		[ViewPage]
		public ActionResult ViewIntlVcApplReturn()
		{
			return View();
		}

		[ExtResult]
		[Description("录入修改公务派车申请信息")]
		public ActionResult SaveOrUpdate()
		{
			FsIntlVehicleAppl entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsIntlVehicleAppl
				{
					ApplUserid = Request["ApplUserid"],
					ApplDate = System.DateTime.Now,
					CarId = Request["CarId"],
					Driver = Request["Driver"],
					Business = Request["Business"],
					Type = Request["Type"],
					PlanPath = Request["PlanPath"],
					MileageOut = Convert.ToDecimal(string.IsNullOrEmpty(Request["MileageOut"]) ? null : Request["MileageOut"]),
					MileageRet = Convert.ToDecimal(string.IsNullOrEmpty(Request["MileageRet"]) ? null : Request["MileageRet"]),
					ExpOutTime = Request["ExpOutTime"] == null ? DateTime.MinValue : Convert.ToDateTime(Request["ExpOutTime"]),
					ExpRetTime = Request["ExpRetTime"] == null ? DateTime.MinValue : Convert.ToDateTime(Request["ExpRetTime"]),
					TtCustName = Request["TtCustName"],
					TtCustTel = Request["TtCustTel"],
					TtCarNo = Request["TtCarNo"],
					TtState = Convert.ToInt32(string.IsNullOrEmpty(Request["TtState"])? "0" : Request["TtState"]),
					TtReason = Request["TtReason"],
					TtFee = Convert.ToDecimal(string.IsNullOrEmpty(Request["TtFee"]) ? null : Request["TtFee"]),
					State = FsIntlVehicleApplState.Editing,  //"待提交"
					CreateUser = User.Identity.Name,
					SmsTime = System.Data.SqlTypes.SqlDateTime.MinValue.Value,
					Description = Request["Description"]
				};
			}
			else
			{
				// get the entity.
				entity = FsIntlVehicleApplFacade.Get(Request["Id"]);
				// set new value..
				entity.ApplUserid = Request["ApplUserid"];
				entity.CarId = Request["CarId"];
				entity.Driver = Request["Driver"];
				entity.Business = Request["Business"];
				entity.Type = Request["Type"];
				entity.PlanPath = Request["PlanPath"];
				entity.MileageOut = Convert.ToDecimal(string.IsNullOrEmpty(Request["MileageOut"]) ? null : Request["MileageOut"]);
				entity.MileageRet = Convert.ToDecimal(string.IsNullOrEmpty(Request["MileageRet"]) ? null : Request["MileageRet"]);
				entity.ExpOutTime = Request["ExpOutTime"] == null ? DateTime.MinValue : Convert.ToDateTime(Request["ExpOutTime"]);
				entity.ExpRetTime = Request["ExpRetTime"] == null ? DateTime.MinValue : Convert.ToDateTime(Request["ExpRetTime"]);
				entity.TtCustName = Request["TtCustName"];
				entity.TtCustTel = Request["TtCustTel"];
				entity.TtCarNo = Request["TtCarNo"];
				entity.TtState = Convert.ToInt32(string.IsNullOrEmpty(Request["TtState"])? "0" : Request["TtState"]);
				entity.TtReason = Request["TtReason"];
				entity.TtFee = Convert.ToDecimal(string.IsNullOrEmpty(Request["TtFee"]) ? null : Request["TtFee"]);
				entity.Description = Request["Description"];
			}
			// 设置行驶里程信息..
			// 保存信息
			FsIntlVehicleApplFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("删除公务派车信息")]
		public ActionResult Delete()
		{
			FsIntlVehicleApplFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("物理删除公务派车信息")]
		public ActionResult PhysicsDelete()
		{
			FsIntlVehicleApplFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("公务派车单提交审批")]
		public ActionResult InitApproveLog()
		{
			foreach (string id in Request["Ids"].Split(','))
			{
				FsIntlVehicleAppl entity = FsIntlVehicleApplFacade.Get(id);
				if (entity.State != FsIntlVehicleApplState.Editing)
				{
					throw new Exception("错误:用车单[" + entity.Id + "]的当前状态是[" + entity.State.ToString() + "].");
				}
				string smsContent = entity.ToSmsString();

				InfraApprove appr = InfraApproveFacade.GetByDeptAndType(entity.IntlVehicle.DepartmentId, "ap_intvappl");

				InfraApproveFacade.InitApproveLog(appr.Id, entity.Id, smsContent);
				entity.State = FsIntlVehicleApplState.Appling;
				FsIntlVehicleApplFacade.SaveOrUpdate(entity);
			}
			return this.JsonFormat(new ExtResult { success = true });
		}

		[ExtResult]
		[Description("公务派车单审批")]
		public ActionResult ApproveAppl()
		{
			if (String.IsNullOrEmpty(Request["Id"]))
				throw new ArgumentNullException("审批步骤ID不能为空.");
			// 审批步骤信息
			InfraApproveLog appLogStep = InfraApproveFacade.GetApproveLogStepById(Request["Id"]);
			appLogStep.InputAprCode = Request["InputAprCode"];
			appLogStep.InputUserLName = Request["InputUserLName"];
			appLogStep.InputUserPwd = Request["InputUserPwd"];
			appLogStep.InputApprState = Request["InputApprState"];
			appLogStep.Description = Request["Description"];
			// 添加审批步骤并返回审批结果
			int step = InfraApproveFacade.ProcedureApprove(appLogStep);
			// 审批通过
			if (step == 0)
			{
				FsIntlVehicleAppl entity = FsIntlVehicleApplFacade.Load(appLogStep.ApproveItemId);
				entity.State = FsIntlVehicleApplState.Agreed;
				FsIntlVehicleApplFacade.SaveOrUpdate(entity);
			}
			else if (step < 0)
			{
				FsIntlVehicleAppl entity = FsIntlVehicleApplFacade.Load(appLogStep.ApproveItemId);
				entity.State = FsIntlVehicleApplState.Refused;
				FsIntlVehicleApplFacade.SaveOrUpdate(entity);
			}

			return this.JsonFormat(new ExtResult { success = true, msg = step.ToString() });
		}

		[ExtResult]
		[Description("派车出发信息登记")]
		public ActionResult IntlVehicleApplSend()
		{
			FsIntlVehicleAppl entity;
			if (String.IsNullOrEmpty(Request["Id"]))
				throw new ArgumentNullException("派车单ID不能为空.");
			entity = FsIntlVehicleApplFacade.Load(Request["Id"]);
			if (entity.State != FsIntlVehicleApplState.Agreed)
				throw new ValidationException("派车单当前状态[" + entity.State.ToString() + "],不是[" + FsIntlVehicleApplState.Agreed.ToString() + "],不能登记出发信息.");
			if (entity.IntlVehicle.State == "外出")
				throw new ValidationException("派车单的车辆状态[" + entity.IntlVehicle.State.ToString() + "],不能登记出发信息.");

			//登记派车时的信息
			entity.Driver = Request["Driver"];
			entity.MileageOut = Convert.ToInt32(string.IsNullOrEmpty(Request["MileageOut"]) ? null : Request["MileageOut"]);
			entity.MileageRet = Convert.ToInt32(string.IsNullOrEmpty(Request["MileageRet"]) ? null : Request["MileageRet"]);
			entity.ActOutTime = System.DateTime.Now;
			//entity.ActRetTime = Request["ActRetTime"];
			entity.TtCustName = Request["TtCustName"];
			entity.TtCustTel = Request["TtCustTel"];
			entity.TtCarNo = Request["TtCarNo"];
			entity.TtState = Convert.ToInt32(string.IsNullOrEmpty(Request["TtState"])? "0" : Request["TtState"]);
			entity.TtReason = Request["TtReason"];
			entity.TtFee = Convert.ToDecimal(string.IsNullOrEmpty(Request["TtFee"]) ? null : Request["TtFee"]);
			entity.Description = Request["Description"];
			entity.State = FsIntlVehicleApplState.Processing;
			entity.IntlVehicle.State = "外出";

			//保存信息返回。
			FsIntlVehicleApplFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("派车返回信息登记")]
		public ActionResult IntlVehicleApplReturn()
		{
			FsIntlVehicleAppl entity;
			if (String.IsNullOrEmpty(Request["Id"]))
				throw new ArgumentNullException("派车单ID不能为空.");
			entity = FsIntlVehicleApplFacade.Load(Request["Id"]);
			if (entity.State != FsIntlVehicleApplState.Processing)
				throw new ValidationException("派车单当前状态[" + entity.State.ToString() + "],不能登记返回信息.");

			//登记派车时的信息
			entity.Driver = Request["Driver"];
			entity.MileageOut = Convert.ToInt32(string.IsNullOrEmpty(Request["MileageOut"]) ? null : Request["MileageOut"]);
			entity.MileageRet = Convert.ToInt32(string.IsNullOrEmpty(Request["MileageRet"]) ? null : Request["MileageRet"]);
			//entity.ActOutTime = System.DateTime.Now.ToString("yyyy年MM月dd日 hh:mm:ss");
			entity.ActRetTime = System.DateTime.Now;
			entity.TtCustName = Request["TtCustName"];
			entity.TtCustTel = Request["TtCustTel"];
			entity.TtCarNo = Request["TtCarNo"];
			entity.TtState = Convert.ToInt32(string.IsNullOrEmpty(Request["TtState"]) ? "0" : Request["TtState"]);
			entity.TtReason = Request["TtReason"];
			entity.TtFee = Convert.ToDecimal(string.IsNullOrEmpty(Request["TtFee"]) ? null : Request["TtFee"]);
			entity.Description = Request["Description"];
			entity.State = FsIntlVehicleApplState.Returned;
			entity.IntlVehicle.State = "待命";

			//保存信息返回。
			FsIntlVehicleApplFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[Description("获取公务派车单信息清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsIntlVehicleApplFacade.GetPlist(
				Request["q_query"],
				Request["state"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsIntlVehicleAppl> { total = total, data = data },
				new string[] { "ApplUser", "IntlVehicle" });
		}

		[Description("获取公务派车单的基本信息")]
		public ActionResult GetById()
		{
			var entity = FsIntlVehicleApplFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsIntlVehicleAppl> { data = new FsIntlVehicleAppl[] { entity } }, new string[] { "ApplUser", "IntlVehicle" }
			   );
		}

		[ExtResult]
		[Description("判断公务派车单的字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsIntlVehicleApplFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
